package com.example.pokemon_chat_room.adapter;

import static android.content.Context.MODE_PRIVATE;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.example.pokemon_chat_room.PokemonDetail;
import com.example.pokemon_chat_room.R;
import com.example.pokemon_chat_room.pojo.Items;
import com.example.pokemon_chat_room.pojo.Pokemon;
import com.example.pokemon_chat_room.request.OkHttp;
import com.example.pokemon_chat_room.util.MyImageView;

import java.io.IOException;
import java.util.List;

public class GMShopAdapter extends RecyclerView.Adapter<GMShopAdapter.ViewHolder> {

    private List<Items> itemsList;
    private View view;
    private Context context;

    public GMShopAdapter(Context context, List<Items> itemsList) {
        this.context = context;
        this.itemsList = itemsList;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private MyImageView item_img;
        private TextView name_tv;

        private TextView amount_tv;

        private TextView user_tv;

        private Button buy_btn;

        public ViewHolder(View view) {
            super(view);
            // Define click listener for the ViewHolder's View

            item_img = view.findViewById(R.id.item_img);
            name_tv = view.findViewById(R.id.name_tv);
            amount_tv = view.findViewById(R.id.amount_tv);
            user_tv = view.findViewById(R.id.user_tv);
            buy_btn  = view.findViewById(R.id.buy_btn);
        }

    }


    @NonNull
    @Override
    public GMShopAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shop_item_layout, parent, false);
        GMShopAdapter.ViewHolder holder = new GMShopAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull GMShopAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.item_img.setImageURL("poke/items/" + itemsList.get(position).getImage());
        holder.name_tv.setText(itemsList.get(position).getName());
        holder.amount_tv.setText(itemsList.get(position).getAmount().toString());
        holder.user_tv.setText("GM");
        holder.buy_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences mShared = context.getSharedPreferences("user", MODE_PRIVATE);
                Double amount = Double.parseDouble(mShared.getString("amount",""));// 从共享参数中获得名叫name的字符串
                if (amount < itemsList.get(position).getAmount()) {
                    Toast.makeText(context, "金额不足", Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(context, "购买成功", Toast.LENGTH_SHORT).show();

                SharedPreferences.Editor editor = mShared.edit();// 获得编辑器的对象
                editor.putString("amount", String.valueOf(amount - itemsList.get(position).getAmount()));
                editor.apply();// 提交编辑器中的修改
                OkHttp okHttp = new OkHttp();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("token", mShared.getString("token", ""));
                        jsonObject.put("itemsId", itemsList.get(position).getId());
                        try {
                            JSONObject res = okHttp.Post(context, "items/buy", jsonObject);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }).start();
            }
        });
    }

    @Override
    public int getItemCount() {
        return itemsList.size();
    }


}
